package jp.gr.java_conf.ryz.summarize.android.view;

import jp.gr.java_conf.ryz.summarize.android.R;
import jp.gr.java_conf.ryz.summarize.android.model.Post;
import android.content.Context;
import android.text.Html;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

public class PostPanel extends LinearLayout
{
	private static String _headerFormat;
	
	private GestureDetector _gestureDetector;
	private Post _post;

	public PostPanel(Context context)
	{
		super(context);
		
		View.inflate(context, R.layout.post_panel, this);
		
		CheckBox selectCheck = (CheckBox)findViewById(R.id.post_panel_selectCheck);
		selectCheck.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			{
				changeSelectCheckState(buttonView, isChecked);
			}
		});
		
		_gestureDetector = new GestureDetector(getContext(), new SimpleOnGestureListener()
		{
			// onDown で true を返さないと少しのタッチでも onLogPress が実行されます。
			@Override
			public boolean onDown(MotionEvent e)
			{
				return true;
			}

			@Override
			public boolean onSingleTapUp(MotionEvent e)
			{
				CheckBox selectCheck = (CheckBox)findViewById(R.id.post_panel_selectCheck);
				boolean newSelectedState = !selectCheck.isChecked();
				changeSelectCheckState(selectCheck, newSelectedState);
				
				return false;
			}

			@Override
			public void onLongPress(MotionEvent e)
			{
				PopupWindow popup = new DecorationPanel(getContext());
				popup.showAtLocation(PostPanel.this, Gravity.CENTER, 0, 0);
			}
		});
	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		if (_gestureDetector.onTouchEvent(event))
			return true;
		
		return super.onTouchEvent(event);
	}


	public void setPost(Post post)
	{
		_post = post;
		
		if (_headerFormat == null)
		{
			_headerFormat = (String)getResources().getText(R.string.post_panel_header);
		}
		
		String header = String.format(
				_headerFormat, post.getNumber(), post.getName(), post.getDate(), post.getMail());
		TextView headerText = (TextView)findViewById(R.id.post_panel_header);
		headerText.setText(Html.fromHtml(header));
		
		TextView bodyText = (TextView)findViewById(R.id.post_panel_body);
		bodyText.setText(Html.fromHtml(post.getBody()));
	}
	
	public Post getPost()
	{
		return _post;
	}
	
	
	private void changeSelectCheckState(CompoundButton button, boolean newValue)
	{
		button.setChecked(newValue);
		if (newValue)
		{
			setBackgroundColor(getResources().getColor(R.color.PostPanel_selected));
		}
		else
		{
			setBackgroundColor(getResources().getColor(R.color.PostPanel_unselected));
		}
	}

}
